//
// Created by yorkc on 2023/12/21.
//

#include "myfriends.h"
using namespace std;
myfriends::myfriends() {

}

void myfriends::json_to_obj(const nlohmann::json &json, myfriends &obj) {
    json.at("name").get_to(obj.Name);
    json.at("id").get_to(obj.ID);
    json.at("age").get_to(obj.Age);
    json.at("major").get_to(obj.Major);
}

void myfriends::obj_to_json(nlohmann::json &json, const myfriends &obj) {
    json["name"]=obj.Name;
    json["id"]=obj.ID;
    json["age"]=obj.Age;
    json["major"]=obj.Major;
}

myfriends * myfriends::db_to_obj(vector<map<std::string, std::string>> &results) {
    myfriends * myfriendsdb= nullptr;
    if(!results.empty()){
        myfriendsdb=new myfriends();
        map<string, string> &row = results.at(0);
        string id = row.at("ID");
        string name = row.at("Name");
        string age = row.at("Age");
        string major = row.at("Major");
        myfriendsdb->ID= stoi(id);
        myfriendsdb->Name=name;
        myfriendsdb->Age= stoi(age);
        myfriendsdb->Major=major;
        return myfriendsdb;
    }
    return myfriendsdb;
}

vector<myfriends> myfriends::db_to_objs(vector<map<std::string, std::string>> &results) {
    vector<myfriends> mfriendsdb;
    if(!results.empty()){
        for (int i = 0; i <results.size(); ++i) {
            myfriends  myfriendsdb;
            map<string, string> &row = results.at(i);
            string id = row.at("ID");
            string name = row.at("Name");
            string age = row.at("Age");
            string major = row.at("Major");
            myfriendsdb.ID= stoi(id);
            myfriendsdb.Name=name;
            myfriendsdb.Age= stoi(age);
            myfriendsdb.Major=major;
            mfriendsdb.push_back(myfriendsdb);
        }
    }
    return mfriendsdb;
}
vector<myfriends *> myfriends::db_to_objsp(vector<map<std::string, std::string>> &results) {
    vector<myfriends*> mfriendsdb;
    if(!results.empty()){
        for (int i = 0; i <results.size(); ++i) {
            myfriends*  myfriendsdb=new myfriends();
            map<string, string> &row = results.at(i);
            string id = row.at("ID");
            string name = row.at("Name");
            string age = row.at("Age");
            string major = row.at("Major");
            myfriendsdb->ID= stoi(id);
            myfriendsdb->Name=name;
            myfriendsdb->Age= stoi(age);
            myfriendsdb->Major=major;
            mfriendsdb.push_back(myfriendsdb);
        }
    }
    return mfriendsdb;
}

int myfriends::getId() const {
    return ID;
}

void myfriends::setId(int id) {
    ID = id;
}

int myfriends::getAge() const {
    return Age;
}

void myfriends::setAge(int age) {
    Age = age;
}

const string &myfriends::getName() const {
    return Name;
}

void myfriends::setName(const string &name) {
    Name = name;
}

const string &myfriends::getMajor() const {
    return Major;
}

void myfriends::setMajor(const string &major) {
    Major = major;
}

